<template>
  <div>
    <h1>我的投诉记录</h1>
    <el-row :gutter="20">
      <el-col :span="1.5"
        ><el-button type="primary" @click="open">投诉</el-button></el-col
      >
    </el-row>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="date" width="180"
        ><i class="el-icon-user"></i
      ></el-table-column>
      <el-table-column prop="title" label="投诉标题" width="180">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column prop="statusName" label="处理状态"> </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button @click="toDetail(scope.row.id)" type="success"
            >查看</el-button
          ></template
        >
      </el-table-column>
    </el-table>

    <!-- Form -->

    <el-dialog title="投诉" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item
          label="投诉标题"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input
            v-model="form.title"
            autocomplete="off"
            :disabled="flag"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="投诉内容"
          :label-width="formLabelWidth"
          prop="content"
        >
          <el-input
            v-model="form.content"
            autocomplete="off"
            :disabled="flag"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="联系人"
          :label-width="formLabelWidth"
          prop="contact"
        >
          <el-input
            v-model="form.contact"
            autocomplete="off"
            :disabled="flag"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="联系人电话"
          :label-width="formLabelWidth"
          prop="contactTel"
        >
          <el-input
            v-model="form.contactTel"
            autocomplete="off"
            :disabled="flag"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit(form)">保 存</el-button>
        <el-button type="primary" @click="a">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listComplain, add } from "./a";

export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {},
      formLabelWidth: "180px",
      flag: false,
      rules: {
        title: [
          { required: true, message: "物料名称不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "投诉内容不能为空", trigger: "blur" }
        ],
        contact: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        contactTel: [
          { required: true, message: "联系人电话不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    reset() {
      this.form = {
        title: null,
        content: null,
        contact: null,
        contactTel: null
      };
      this.resetForm("form");
    },
    cancel() {
      this.dialogFormVisible = false;
      this.flag = false;
    },

    a() {
      this.flag = true;
    },
    toDetail(id) {
      const url = this.$router.resolve({
        path: "/complaint/detail",
        query: {
          id
        }
      });
      window.open(url.href, "_blank");
    },
    submit() {
      // console.log("form", form);
      // console.log("this.form", this.form);
      this.flag = false;
      this.$refs["form"].validate(valid => {
        if (valid) {
          add(this.form).then(response => {
            this.msgSuccess("提交成功");
            this.dialogFormVisible = false;
            this.getList();
          });
        }
      });

      this.reset();
    },
    open() {
      this.dialogFormVisible = true;
    },
    getList() {
      listComplain().then(res => {
        console.log(res);
        this.tableData = res.rows;
        console.log(this.tableData);
      });
    }
  }
};
</script>
